import { extend } from 'umi-request';
import storageUtils from '../utils/storageUtils';
const request = extend({
  // 请求前缀
  prefix: 'http://xawn.f3322.net:8012',
  // suffix:"?id=1",
  timeout: 2000,
  errorHandler: (error) => {
    console.log('请求错误', error);
  },
});

request.interceptors.request.use((url, options) => {
  let token = (storageUtils.getStorage('token'))
  if (token) {
    options.headers = {
      'Content-Type':
        options.method?.toLocaleLowerCase() === 'get'
          ? 'application/json;charset=UTF-8'
          : 'application/x-www-form-urlencoded',
      Authentication: token,
      ...options.headers,
    };
    return {
      url: `${url}`,
      options: options,
    };
  } else {
    options.headers= {
      'Content-Type': 'application/x-www-form-urlencoded',
    };
    return {
      url: `${url}`,
      options: options,
    };
  }
});
export default request;
